package com.btg.sql;

import com.btg.sql.util.SqlHandler;
import com.btg.sql.util.SqlHelp;

import java.util.Map;

/**
 * Created by Administrator on 2016/12/29.
 */
public class SqlManager {

    private static SqlManager sqlManager = null;

    private String xmlPath = null;

    private boolean devMode = false;

    private SqlManager() {
    }

    private SqlManager(String xmlPath) {
        this.xmlPath = xmlPath;
    }

    private SqlManager(String xmlPath, boolean devMode) {
        this.xmlPath = xmlPath;
        this.devMode = devMode;
    }

    public static SqlManager init(String xmlPath) {
        if (sqlManager == null) {
            synchronized (SqlManager.class) {
                if (sqlManager == null) {
                    sqlManager = new SqlManager(xmlPath);
                }
            }
        }
        return sqlManager;
    }

    public static SqlManager init(String xmlPath, boolean devMode) {
        if (sqlManager == null) {
            synchronized (SqlManager.class) {
                if (sqlManager == null) {
                    sqlManager = new SqlManager(xmlPath, devMode);
                }
            }
        }
        return sqlManager;
    }

    public static SqlManager instance() {
        return sqlManager;
    }

    public SqlManager setDevMode(boolean devMode) {
        this.devMode = devMode;
        return init(this.xmlPath, this.devMode);
    }

    public boolean getDevMode() {
        return this.devMode;
    }


    public SqlManager setXmlPath(String xmlPath) {
        this.xmlPath = xmlPath;
        return init(this.xmlPath);
    }

    public String getXmlPath() {
        return this.xmlPath;
    }


    /**
     * 根据传入参数取得原始（未处理过）sql
     *
     * @param sqlId
     * @param paraMap
     * @return
     */
    public String getSqlNative(String sqlId, Map<String, Object> paraMap) {
        return SqlHandler.getSqlNative(sqlId, paraMap);
    }

    /**
     * 根据传入参数取得原始（未处理过）sql
     *
     * @param sqlId
     * @return
     */
    public String getSqlNative(String sqlId) {
        return getSqlNative(sqlId, null);
    }

    /**
     * 根据传入参数取得处理后的sql及数组
     *
     * @param sqlId
     * @param paraMap
     * @return
     */
    public SqlHelp getSqlHandler(String sqlId, Map<String, Object> paraMap) {
        return SqlHandler.getSqlHandler(sqlId, paraMap);
    }

}
